package com.utf999.qiangmanger.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.iface.IOrderTypeDAO;
import com.utf999.qiangmanger.entity.orm.CompanyEntity;
import com.utf999.qiangmanger.entity.orm.OrderTypeEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.AbstractService;
import com.utf999.qiangmanger.service.iface.IOrderTypeService;

/**
 * @author yinwenjie
 *
 */
@Component("OrderTypeServiceImpl")
public class OrderTypeServiceImpl extends AbstractService<OrderTypeEntity, IOrderTypeDAO> implements IOrderTypeService {
	
	/**
	 * 
	 */
	@Autowired
	private IOrderTypeDAO orderTypeDAO;
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.AbstractService#getBaseDAO()
	 */
	@Override
	protected IOrderTypeDAO getBaseDAO() {
		return this.orderTypeDAO;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#queryOrderTypesByCompanyid(java.lang.String)
	 */
	@Override
	public List<OrderTypeEntity> queryOrderTypesByCompanyid(String companyid) throws BizException {
		if(StringUtils.isEmpty(companyid)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E501);
		}
		
		return this.orderTypeDAO.queryOrderTypesByCompanyid(companyid);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#queryOrderTypeByCode(java.lang.String, java.lang.String)
	 */
	@Override
	public OrderTypeEntity queryOrderTypeByCode(String companyid, String orderCode) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(orderCode)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E501);
		}
		
		return this.orderTypeDAO.queryOrderTypeByCode(companyid, orderCode);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#existOrderTypeByCode(java.lang.String, java.lang.String)
	 */
	@Override
	public OrderTypeEntity existOrderTypeByCode(String companyid, String orderCode) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(orderCode)) {
			throw new BizException("错误的参数信息，请检查！", ResponseCode.E501);
		}
		
		return this.orderTypeDAO.existOrderTypeByCode(companyid, orderCode);
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#initDefaultOrderType(com.utf999.qiangmanger.entity.orm.CompanyEntity)
	 */
	@Override
	public void initDefaultOrderType(CompanyEntity company) throws BizException {
		//消费服务单
		OrderTypeEntity type = new OrderTypeEntity();
		type.setCompany(company);
		type.setCreateType(1);
		type.setDuration(180);
		type.setIntegral(20);
		type.setOrderTypeName("消费服务单");
		type.setStatus(1);
		type.setOrderCode("0001");
		this.insert(type);
		//维修服务单
		type = new OrderTypeEntity();
		type.setCompany(company);
		type.setCreateType(1);
		type.setDuration(180);
		type.setIntegral(20);
		type.setOrderTypeName("维修服务单");
		type.setStatus(1);
		type.setOrderCode("0002");
		this.insert(type);
		//保洁服务单
		type = new OrderTypeEntity();
		type.setCompany(company);
		type.setCreateType(1);
		type.setDuration(180);
		type.setIntegral(20);
		type.setOrderTypeName("保洁服务单");
		type.setStatus(1);
		type.setOrderCode("0003");
		this.insert(type);
		//普通服务单
		type = new OrderTypeEntity();
		type.setCompany(company);
		type.setCreateType(1);
		type.setDuration(180);
		type.setIntegral(20);
		type.setOrderTypeName("普通服务单");
		type.setStatus(1);
		type.setOrderCode("0004");
		this.insert(type);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#createOrderType(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public void createOrderType(String companyid, String orderTypeName, String orderCode , Integer integral, Integer duration) throws BizException {
		if(StringUtils.isEmpty(companyid) || StringUtils.isEmpty(orderTypeName)
			|| integral == null || duration == null) {
			throw new BizException("错误的参数，请检查！", ResponseCode.E404);
		}
		
		OrderTypeEntity type = new OrderTypeEntity();
		//公司信息
		CompanyEntity company = new CompanyEntity();
		company.setUid(companyid);
		type.setCompany(company);
		//创建类型，一定是2
		type.setCreateType(2);
		type.setDuration(duration);
		type.setIntegral(integral);
		type.setOrderTypeName(orderTypeName);
		type.setStatus(1);
		type.setOrderCode(orderCode);
		
		this.insert(type);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#modifyOrderType(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public void modifyOrderType(String orderTypeid, String orderTypeName, String orderCode,  Integer integral, Integer duration) throws BizException {
		if(StringUtils.isEmpty(orderTypeid) || StringUtils.isEmpty(orderTypeName)
			|| integral == null || duration == null || StringUtils.isEmpty(orderCode)) {
			throw new BizException("错误的参数，请检查！", ResponseCode.E404);
		}
		OrderTypeEntity type = (OrderTypeEntity)this.getEntity(orderTypeid);
		if(type == null) {
			throw new BizException("没有发现指定的工单类型！", ResponseCode.E403);
		}
		
		type.setDuration(duration);
		type.setIntegral(integral);
		type.setOrderTypeName(orderTypeName);
		type.setOrderCode(orderCode);
		this.update(type);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#bindUsergroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void bindUsergroupMapping(String ordertypeid, String... groupids) throws BizException {
		if(StringUtils.isEmpty(ordertypeid) 
			|| groupids == null || groupids.length == 0) {
			throw new BizException("错误的参数，请检查！", ResponseCode.E404);
		}
		
		this.orderTypeDAO.bindUsergroupMapping(ordertypeid, groupids);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.service.iface.IOrderTypeService#unbindUsergroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void unbindUsergroupMapping(String ordertypeid, String... groupids) throws BizException {
		if(StringUtils.isEmpty(ordertypeid) 
			|| groupids == null || groupids.length == 0) {
			throw new BizException("错误的参数，请检查！", ResponseCode.E404);
		}
		
		this.orderTypeDAO.unbindUsergroupMapping(ordertypeid, groupids);
	}
}
